package com.jbp.controller;

import com.jbp.exception.RRException;
import com.jbp.model.Room;
import com.jbp.utils.R;
import com.jbp.utils.qiniuyun.Qiniuyun;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.UUID;

@Api(tags ="APP接口--图片上传" )
@ApiIgnore
@RestController
@RequestMapping("/rest/file")
public class UpFileController{

   /* @ApiOperation(value="图片上传",notes="图片上传")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "token",paramType = "query"),
            @ApiImplicitParam(name="sessionId",value = "sessionId",paramType = "query"),
            @ApiImplicitParam(name="file",value = "file",paramType = "query"),
    })
    @RequestMapping(value = "/upFile", method = RequestMethod.POST)
    public R upFile(@RequestParam("file") MultipartFile file) {
        String fileNameNow = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String key = UUID.randomUUID().toString() + fileNameNow;
        try {
            InputStream inputStream = file.getInputStream();
            Qiniuyun.upInput(key, inputStream);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("上传异常，请联系管理员");
        }
        return R.ok((Object) key);
    }*/

    /**获取七牛云上传token*/
    @GetMapping(value = "/getToken")
    public R getToken() {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("token",Qiniuyun.getToken());
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("上传异常，请联系管理员");
        }
    }
}
